package controller.nonMember;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.CinemaUser;
import model.Member;
import util.HibernateUtil;
import util.MD5_Encoder;

public class RegistrationServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String action = request.getParameter("action");
            if (action == null) {
                defaultAction(request, response);
            } else if (action.equalsIgnoreCase("register")) {
                registerMember(request, response);
            } else {
                defaultAction(request, response);
            }
        } catch (Exception e) {
            HttpSession session = request.getSession();
            session.setAttribute("registerErrorMessage", e.getMessage());
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/nonMember/registration.jsp");
            requestDispatcher.forward(request, response);
        }
    }

    private void defaultAction(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/nonMember/registration.jsp");
        requestDispatcher.forward(request, response);
    }

    private void registerMember(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        String newUsername = request.getParameter("newUsername");
        String truePassword = request.getParameter("newPassword");
        String md5_password = MD5_Encoder.encode(truePassword);

        // insert into CinemaUser
        CinemaUser newUser = new CinemaUser();
        newUser.setUsername(newUsername);
        newUser.setPassword(md5_password);
        newUser.setRole("member");

        boolean userInsertSuccess=HibernateUtil.insert(newUser);
        if(userInsertSuccess == false)
            throw new Exception("User name \""+newUsername+"\" has been used");

        // insert into member
        Member newMember = new Member();
        newMember.setUserName(newUsername);
        newMember.setLoyaltyPoint(0);

        boolean memberInsertSuccess=HibernateUtil.insert(newMember);
        if(memberInsertSuccess == false)
            throw new Exception("User name \""+newUsername+"\" has been used");

        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/nonMember/registrationResult.jsp");
        requestDispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
